package com.migu.rbac.proxy;

import com.alibaba.fastjson.JSONObject;

import com.migu.rbac.message.PromactityService;
import com.migu.rbac.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhang on 2018/9/1 13:00
 */
@Component
public class PromactityProxy {

    public static final Logger logger = LoggerFactory.getLogger(PromactityProxy.class);

    @Autowired
    private PromactityService promactityService;

    @Value("${sendmessage.fromID}")
    private String FROMID;

    @Value("${sendmessage.tempCode}")
    private String TEMPCODE;


    /**
     * 调用营销发送短信
     *
     * @param phoneNum
     * @return
     */
    public Map<String, String> sendMessage(String phoneNum) {

        //响应结果
        Map<String, String> resMap = new HashMap<>();

        //调用营销接口
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("fromID", FROMID);
        paramsMap.put("phoneNum", phoneNum);
        paramsMap.put("tempCode", TEMPCODE);

        try {
            String result = promactityService.sendMessage(paramsMap);
            //解析响应报文
            resMap = analyzeResult(result);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("PromactityProxy.sendMessage" + e.getMessage());
            resMap.put("message", "fail");
            //lego自定义发送验证码失败code
            resMap.put("code", "0000");
            return resMap;
        }

    }


    /**
     * 调用营销校验短信
     *
     * @param phoneNum
     * @return
     */
    public Map<String, String> checkMessage(String phoneNum, String smsCode) {

        //响应结果
        Map<String, String> resMap = new HashMap<>();

        if (StringUtils.isNullStr(phoneNum) || StringUtils.isNullStr(smsCode)) {
            logger.info("params is empty!");
            resMap.put("message", "fail");
            return resMap;
        }

        //调用营销接口
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("fromID", FROMID);
        paramsMap.put("phoneNum",phoneNum);
        paramsMap.put("smsCode",smsCode);

        try {
            String result = promactityService.checkMessage(paramsMap);
            //解析响应报文
            resMap = analyzeResult(result);
            return resMap;

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("PromactityProxy.checkMessage" + e.getMessage());
            resMap.put("message", "error");
            return resMap;
        }

    }


    /**
     * 解析请求结果
     *
     * @param result 结果
     * @return 解析结果
     */
    private Map<String, String> analyzeResult(String result) {


        Map<String, String> resMap = new HashMap<>();
        if (StringUtils.isNullStr(result)) {
            resMap.put("mesage", "fail");
            resMap.put("code", "0000");
            logger.info("请求结果为空");
            return resMap;
        }
        String message = "fail";
        JSONObject jsonObject = JSONObject.parseObject(result);
        logger.info(">>>>>>>>调用营销平台请求结果：" + jsonObject.toString());

        //请求成功，code为1
        if (jsonObject.getString("code").equals("1")) {
            message = "success";
            logger.info("<<<<<<<<<<<<返回结果描述：" + jsonObject.getString("message"));
        }
        //发送短信超过限制
        else if (jsonObject.getString("code").equals("4001")) {
            message = "fail";
        }
        resMap.put("code", jsonObject.getString("code"));
        resMap.put("message", message);
        return resMap;
    }
}
